package base.tools;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import base.tools.Tools;

/**
 * @author lcl
 * 
 */
public class Scan {
	private ActionMapping actionMapping;
	private ActionForm actionForm;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	private Sql sql;

	public Scan(ActionMapping actionMapping, ActionForm actionForm,
			HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse, Sql sql) {
		this.actionMapping = actionMapping;
		this.actionForm = actionForm;
		this.request = httpServletRequest;
		this.response = httpServletResponse;
		this.sql = sql;
	}

	public Scan(ActionMapping actionMapping,
			HttpServletRequest httpServletRequest) {
		this.actionMapping = actionMapping;
		this.request = httpServletRequest;
	}

	public Scan(HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) {

		this.request = httpServletRequest;
		this.response = httpServletResponse;
	}

	public void setSql(Sql sql) {
		this.sql = sql;
	}


	public void close() {
		try {
			this.sql.close();
		} catch (Exception e) {
		}
		;
	}

	public ActionMapping getActionMapping() {
		return actionMapping;
	}

	public ActionForm getActionForm() {
		return actionForm;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public HttpSession getSession() {
		return request.getSession(true);
	}

	public Sql getSql() {
		if (sql == null) {
			sql = new Sql();
		}
		return sql;
	}


	public String getParameter(String parametername) {
		String resStr = "";
		if (request.getParameter(parametername) != null
				&& request.getParameter(parametername).length() > 0) {
						
			try {
				resStr = new String((request.getParameter(parametername)).getBytes ("ISO-8859-1"),"utf8" ) ;
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return resStr;
	}

	public String getParameter(String parametername, String defaultValue) {
		String ret = "";

		if (request.getParameter(parametername) != null
				&& !request.getParameter(parametername).equals("")) {
			ret = request.getParameter(parametername);
			ret = Tools.replace(ret, "'", "''");
			ret = ret.trim();
		} else {
			ret = defaultValue;
		}

		return ret;
	}
	public void setAttribute(String paraName, Object obj) {
		request.setAttribute(paraName, obj);
	}

	public ActionForward findForward(String forward) {
		return actionMapping.findForward(forward);
	}

	public ActionForward findForward(String jspName, boolean ifforward) {
		return new ActionForward(jspName, jspName, ifforward);
	}

	public void debug(String str) {
		try {
			response.getWriter().println(str);
		} catch (Exception e) {
			try {
				response.getWriter().println("system error<!--");
				e.printStackTrace(response.getWriter());
				response.getWriter().println("-->");
			} catch (Exception ee) {

			}
		}
	}

	public void debug(int str) {
		try {

			response.getWriter().println(str);
		} catch (Exception e) {
			try {
				response.getWriter().println("system error<!--");
				e.printStackTrace(response.getWriter());
				response.getWriter().println("-->");
			} catch (Exception ee) {

			}
		}
	}

	public void debug(boolean str) {
		try {

			response.getWriter().println(str);
		} catch (Exception e) {
			try {
				response.getWriter().println("system error<!--");
				e.printStackTrace(response.getWriter());
				response.getWriter().println("-->");
			} catch (Exception ee) {

			}
		}
	}

	public HashMap<String,String> getMapFromSql(String sql) throws Exception {
		return this.sql.getMapFromSqlByPool(sql);
	}


	public ArrayList<HashMap<String,String>> getListFromSql(String sql) throws Exception {
		return this.sql.getListFromSqlByPool(sql);
	}


	public void getResultSet(String sql) throws Exception {
		this.sql.getRSFromSqlByPool(sql);
	}

	public boolean getRsNext() {
		try {
			return this.sql.getRsNext();
		} catch (SQLException e) {
			debug("<!--Method scan.getRsNext() has error:" + e.getMessage()
					+ "-->");
		}
		return false;
	}

	public String getRsString(String fieldName) {
		String str = "";
		try {
			str = this.sql.getRsString(fieldName);
			if (str == null)
				str = "";
			else
				str = (str);
		} catch (SQLException e) {
			debug("<!--Method scan.getRsString(String fieldName) has error:"
					+ e.getMessage() + "-->");
		}
		return str;
	}

	public String getRsString(String fieldName, String defaultValue) {
		String str = "";
		try {
			str = this.sql.getRsString(fieldName);
			if (str != null && !str.equals(""))
				str = (str);
			else
				str = defaultValue;
		} catch (SQLException e) {
			debug("<!--Method scan.getRsString(String fieldName,String defaultValue) has error:"
					+ e.getMessage() + "-->");
		}
		return str;
	}

	public String getRsString(int fielInt) {
		String str = "";
		try {
			str = this.sql.getRsString(fielInt);
			if (str == null)
				str = "";
			else
				str = (str);
		} catch (SQLException e) {
			debug("<!--Method scan.getRsString(int fielInt) has error:"
					+ e.getMessage() + "-->");
		}
		return str;
	}

	public int getRsInt(String fieldName) {
		try {
			return this.sql.getRsInt(fieldName);
		} catch (SQLException e) {
			debug("<!--Method scan.getRsInt(String fieldName) has error:"
					+ e.getMessage() + "-->");
		}
		return -1;
	}

	public int getRsInt(int fielInt) {
		try {
			return this.sql.getRsInt(fielInt);
		} catch (SQLException e) {
			debug("<!--Method scan.getRsInt(int fielInt) has error:"
					+ e.getMessage() + "-->");
		}
		return -1;
	}

	public float getRsFloat(String fieldName) {
		try {
			return this.sql.getRsFloat(fieldName);
		} catch (SQLException e) {
			debug("<!--Method scan.getRsFloat(String fieldName) has error:"
					+ e.getMessage() + "-->");
		}
		return -1;
	}

	public float getRsFloat(int fielInt) {
		try {
			return this.sql.getRsFloat(fielInt);
		} catch (SQLException e) {
			debug("<!--Method scan.getRsFloat(int fielInt) has error:"
					+ e.getMessage() + "-->");
		}
		return -1;
	}

	public int executeSQL(String sql) throws Exception {
		return this.sql.executeUpdateByPool(sql);
	}

	public Statement getStatement() throws Exception {
		return this.sql.getStatementByPool();
	}



	public void renderJson(String text) {
		render("application/json;charset=UTF-8", text);
	}


	public void renderXml(String text) {
		render("text/xml;charset=UTF-8", text);
	}


	public void render(String contentType,String text) {
		response.setContentType(contentType);
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			response.getWriter().write(text);
		} catch (IOException e) {

		}
	}

}
